<?php

namespace app\admin\controller;
use think\Db;

/**
 * 链接管理控制器
 * Class Link
 * @package app\admin\controller
 */
class Link extends Common
{
    private $db;
    public function __construct(){
        parent::__construct();
        $this->db = new \app\common\model\Link();
    }
    //首页
    public function index(){
        //2.查询数据
        $data = db('link')->select();
        //3.分配变量
        $this->assign('data',$data);
        //1.载入模版
        return view();
    }
    //添加
    public function add(){
        if(request()->isPost()){
            $res = $this->db->add(input('post.'));
            if($res['valid']){
                $this->success($res['msg'],'index');
            }else{
                $this->error($res['msg']);
            }
        }
        return view();
    }
    //编辑
    public function newedit(){
        $lid = input('param.lid');
        //2.获取旧数据
        $data = Db::name('link')->select($lid);
        //3.分配变量
        $this->assign('data',$data[0]);
        //4.编辑
        if(request()->isPost()){
            $res = $this->db->newedit(input('post.'));
            if($res['valid']){
                $this->success($res['msg'],'index');
            }else{
                $this->error($res['msg']);
            }
        }
        //1.载入模版
        return view();
    }
    //删除
    public function del(){
        $lid = input('param.lid');
        $row = db('link')->delete($lid);
        if($row){
            $this->success('删除成功','index');
        }else{
            $this->error('删除失败');
        }
    }
}
